﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AppData;
using System.Data;
using System.Data.SqlClient;
using AppFormat;

namespace BFOAuction.DAL.Product
{
    public class DAL_ProductType
    {
        public AppFormat.MyError LoadInfo(ref DTO.Product.ProductType objProductType, string strs_ID)
        {
            AppFormat.MyError Error = new AppFormat.MyError();
            ConnectData objCon = new ConnectData();
            try
            {
                objCon.CreateConnect();
                SqlParameter[] p = new SqlParameter[1];
                p[0] = new SqlParameter("@s_ID", strs_ID);
                DataTable dt = objCon.getTableProcedure("CA_ProductType_SEL", p);
                if (dt != null && dt.Rows.Count > 0)
                {
                    objProductType = new DTO.Product.ProductType();
                    objProductType.s_ID = FormatData.IsNull(dt.Rows[0]["s_ID"].ToString(), "").ToString();
                    objProductType.ProductTypeID = FormatData.IsNull(dt.Rows[0]["ProductTypeID"].ToString(), "").ToString();
                    objProductType.ProductTypeName = FormatData.IsNull(dt.Rows[0]["ProductTypeName"].ToString(), "").ToString();
                    objProductType.Note = FormatData.IsNull(dt.Rows[0]["Note"].ToString(), "").ToString();
                }
            }
            catch (Exception ex)
            {
                objCon.ClosedConnect();
                Error.IsError = true;
                Error.TypeError = AppFormat.MyError.enTypeError.Select;
                Error.MessageErrorDetail = objCon.ReturnMyError.MessageErrorDetail;
                Error.MessageError = "Lỗi lấy thông tin nhóm hàng ( " + ex.Message + " )";
            }
            finally
            {
                objCon.ClosedConnect();
            }
            return Error;
        }

        public AppFormat.MyError Insert(DTO.Product.ProductType objProductType)
        {
            AppFormat.MyError Error = new AppFormat.MyError();
            ConnectData objCon = new ConnectData();
            try
            {
                objCon.StrConnect = "data source=LANGTUBUON-PC;initial catalog=BFOAuction;user id=sa;password=saa;";
                objCon.CreateConnect();
                SqlParameter[] p = new SqlParameter[3];
                p[0] = new SqlParameter("@" + DTO.Product.ProductType.colProductTypeID, objProductType.ProductTypeID);
                p[1] = new SqlParameter("@" + DTO.Product.ProductType.colProductTypeName, objProductType.ProductTypeName);
                p[2] = new SqlParameter("@" + DTO.Product.ProductType.colNote, objProductType.Note);

                objCon.execProc("CA_ProductType_ADD", p);

            }
            catch (Exception ex)
            {
                objCon.ClosedConnect();
                Error.IsError = true;
                Error.TypeError = AppFormat.MyError.enTypeError.Select;
                Error.MessageErrorDetail = objCon.ReturnMyError.MessageErrorDetail;
                Error.MessageError = "Lỗi thêm thông tin nhóm hàng ( " + ex.Message + " )";
            }
            finally
            {
                objCon.ClosedConnect();
            }
            return Error;
        }

        public AppFormat.MyError Update(DTO.Product.ProductType objProductType)
        {
            AppFormat.MyError Error = new AppFormat.MyError();
            ConnectData objCon = new ConnectData();
            try
            {
                objCon.CreateConnect();
                SqlParameter[] p = new SqlParameter[4];
                p[0] = new SqlParameter("@s_ID", objProductType.s_ID);
                p[1] = new SqlParameter("@" + DTO.Product.ProductType.colProductTypeID, objProductType.ProductTypeID);
                p[2] = new SqlParameter("@" + DTO.Product.ProductType.colProductTypeName, objProductType.ProductTypeName);
                p[3] = new SqlParameter("@" + DTO.Product.ProductType.colNote, objProductType.Note);

                objCon.execProc("CA_ProductType_UPD", p);

            }
            catch (Exception ex)
            {
                objCon.ClosedConnect();
                Error.IsError = true;
                Error.TypeError = AppFormat.MyError.enTypeError.Select;
                Error.MessageErrorDetail = objCon.ReturnMyError.MessageErrorDetail;
                Error.MessageError = "Lỗi cập nhật thông tin nhóm hàng ( " + ex.Message + " )";
            }
            finally
            {
                objCon.ClosedConnect();
            }
            return Error;
        }

        public AppFormat.MyError Delete(string strs_ID)
        {
            AppFormat.MyError Error = new AppFormat.MyError();
            ConnectData objCon = new ConnectData();
            try
            {
                objCon.StrConnect = "data source=LANGTUBUON-PC;initial catalog=BFOAuction;user id=sa;password=saa;";
                objCon.CreateConnect();
                SqlParameter[] p = new SqlParameter[1];
                p[0] = new SqlParameter("@s_ID", strs_ID);

                objCon.execProc("CA_ProductType_DEL", p);

            }
            catch (Exception ex)
            {
                objCon.ClosedConnect();
                Error.IsError = true;
                Error.TypeError = AppFormat.MyError.enTypeError.Select;
                Error.MessageErrorDetail = objCon.ReturnMyError.MessageErrorDetail;
                Error.MessageError = "Lỗi xóa thông tin nhóm hàng ( " + ex.Message + " )";
            }
            finally
            {
                objCon.ClosedConnect();
            }
            return Error;
        }

        public AppFormat.MyError Select(ref DataTable dtResul, string strs_ID)
        {
            AppFormat.MyError Error = new AppFormat.MyError();

            ConnectData objCon = new ConnectData();
            try
            {
                objCon.StrConnect = "data source=LANGTUBUON-PC;initial catalog=BFOAuction;user id=sa;password=saa;";
                objCon.CreateConnect();
                SqlParameter[] p = new SqlParameter[1];
                p[0] = new SqlParameter("@s_ID", strs_ID);

                dtResul = objCon.getTableProcedure("CA_ProductType_SEL", p);

            }
            catch (Exception ex)
            {
                objCon.ClosedConnect();
                Error.IsError = true;
                Error.TypeError = AppFormat.MyError.enTypeError.Select;
                Error.MessageErrorDetail = objCon.ReturnMyError.MessageErrorDetail;
                Error.MessageError = "Lỗi nạp thông tin nhóm hàng ( " + ex.Message + " )";
            }
            finally
            {
                objCon.ClosedConnect();
            }
            return Error;
        }

        public AppFormat.MyError Search(ref DataTable dtResul)
        {
            AppFormat.MyError Error = new AppFormat.MyError();
            ConnectData objCon = new ConnectData();
            try
            {
                objCon.StrConnect = "data source=LANGTUBUON-PC;initial catalog=BFOAuction;user id=sa;password=saa;";
                objCon.CreateConnect();                
                dtResul = objCon.getTableProcedure("CA_ProductType_SRH");

            }
            catch (Exception ex)
            {
                objCon.ClosedConnect();
                Error.IsError = true;
                Error.TypeError = AppFormat.MyError.enTypeError.Select;
                Error.MessageErrorDetail = objCon.ReturnMyError.MessageErrorDetail;
                Error.MessageError = "Lỗi tìm kiếm thông tin nhóm hàng ( " + ex.Message + " )";
            }
            finally
            {
                objCon.ClosedConnect();
            }
            return Error;
        }
    }
}
